/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package ecarx.xsf.notificationcenter.sdk;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;


/**
 * FileName: BaseServiceConnection.java <br>
 * Description : 连接服务端业务实现 <br>
 * Create Time: 2019/12/25 17:17<br>
 * Update Time: 2021/8/3 14:07 <br>
 *
 * @since 1.0.0
 */
public class EASBaseServiceConnection {

    private static final String TAG = "BaseLCServiceConnection";
    private static final String SERVICE_PACKAGE_NAME = "ecarx.xsf.notificationcenter";
    private static final String ACTION_SERVICE = "android.intent.action.EAS_NOTIFICATION_SERVICE";

    Context mContext;
    EASInitCallback mCallback;
    private boolean bindSuccess;

    private final EASLauncherControllerAPI api;

    public EASBaseServiceConnection(Context context, EASLauncherControllerAPI api, EASInitCallback callback) {
        this.mContext = context;
        this.api = api;
        mCallback = callback;
    }

    ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected: " + name);
            final IEASLCService ieaslcService = IEASLCService.Stub.asInterface(service);
            try {
                ieaslcService.asBinder().linkToDeath(mDeathRecipient, 0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            api.setEASLCService(ieaslcService);
            if (mCallback != null) {
                mCallback.onInit(true);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected() called with: name = [" + name + "]");
            bindSuccess = false;
        }

        @Override
        public void onBindingDied(ComponentName name) {
            Log.e(TAG, "onBindingDied() called with: name = [" + name + "]");
            bindSuccess = false;
            reconnectService();
        }
    };
    IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            bindSuccess = false;
            reconnectService();
        }
    };

    /**
     * * 服务重连
     *
     * @since 1.0.0
     */
    private void reconnectService() {
        if (api.getEASLCService() == null) {
            connectService();
            return;
        }

        Log.i(TAG, "lc binder died");
        api.getEASLCService().asBinder().unlinkToDeath(mDeathRecipient, 0);
        api.setEASLCService(null);
        connectService();
    }

    /**
     * 服务连接.
     *
     * @since 1.0.0
     */
    void connectService() {
        Log.i(TAG, "connectService ");
        Intent intent = new Intent();
        intent.setPackage(SERVICE_PACKAGE_NAME);
        intent.setAction(ACTION_SERVICE);
        bindSuccess = mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    public boolean isBindSuccess() {
        return bindSuccess;
    }

    /**
     * 释放链路
     */
    public void release() {
        if (mContext == null || api.getEASLCService() == null) {
            logConnectError();
            return;
        }
        Log.i(TAG, "release lc eas connection");
        api.getEASLCService().asBinder().unlinkToDeath(mDeathRecipient, 0);
        api.setEASLCService(null);
        if (mConnection != null) {
            mContext.unbindService(mConnection);
        }

    }

    private void logConnectError() {
        Log.e(TAG, "eas lc service is null");
    }
}
